package com.example.android.models

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Paint.FontMetricsInt
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.text.style.DynamicDrawableSpan

class ShapeSpan(var bgColor: Int, var roundSize: Float) : DynamicDrawableSpan() {
    var size = 0
    var mRectF: RectF? = null
    var textColor = -1
    var paddingLeft = 0
    var paddingRight = 0
    var paddingTop = 0
    var paddingBottom = 0
    var marginLeft = 0
    var marginTop = 0
    var marginRight = 0
    var marginBottom = 0
    override fun getDrawable(): Drawable? {
        return null
    }

    override fun getSize(
        paint: Paint,
        text: CharSequence,
        start: Int,
        end: Int,
        fm: FontMetricsInt?
    ): Int {
        size = paint.measureText(text.subSequence(start, end).toString())
            .toInt() + paddingLeft + paddingRight + marginLeft + marginRight
        return size
    }

    override fun draw(
        canvas: Canvas,
        text: CharSequence,
        start: Int,
        end: Int,
        x: Float,
        top: Int,
        y: Int,
        bottom: Int,
        paint: Paint
    ) {
        val oldColor = paint.color
        paint.color = bgColor
        if (mRectF == null) {
            mRectF = RectF()
        }
        mRectF!![x + marginLeft, (top - marginTop).toFloat(), x + size - marginRight] =
            (bottom + marginBottom).toFloat()
        canvas.drawRoundRect(mRectF!!, roundSize, roundSize, paint)
        if (textColor == -1) {
            paint.color = oldColor
        } else {
            paint.color = textColor
        }
        canvas.drawText(text, start, end, x + marginLeft + paddingLeft, y.toFloat(), paint)
    }

    @JvmName("setPaddingLeft1")
    fun setPaddingLeft(paddingLeft: Int) {
        this.paddingLeft = paddingLeft
    }

    @JvmName("setPaddingRight1")
    fun setPaddingRight(paddingRight: Int) {
        this.paddingRight = paddingRight
    }

    @JvmName("setPaddingTop1")
    fun setPaddingTop(paddingTop: Int) {
        this.paddingTop = paddingTop
    }

    @JvmName("setPaddingBottom1")
    fun setPaddingBottom(paddingBottom: Int) {
        this.paddingBottom = paddingBottom
    }

    @JvmName("setMarginLeft1")
    fun setMarginLeft(marginLeft: Int) {
        this.marginLeft = marginLeft
    }

    @JvmName("setMarginTop1")
    fun setMarginTop(marginTop: Int) {
        this.marginTop = marginTop
    }

    @JvmName("setMarginRight1")
    fun setMarginRight(marginRight: Int) {
        this.marginRight = marginRight
    }

    @JvmName("setMarginBottom1")
    fun setMarginBottom(marginBottom: Int) {
        this.marginBottom = marginBottom
    }

    @JvmName("setBgColor1")
    fun setBgColor(bgColor: Int) {
        this.bgColor = bgColor
    }

    @JvmName("setTextColor1")
    fun setTextColor(textColor: Int) {
        this.textColor = textColor
    }

    @JvmName("setRoundSize1")
    fun setRoundSize(roundSize: Float) {
        this.roundSize = roundSize
    }
}